<?php
require_once __DIR__ . '/../Protocols/JsonProtocol.php';
ini_set('display_errors', 'on');
error_reporting(E_ALL);

$ip = isset($argv[1]) ? $argv[1] : '127.0.0.1';
$port = isset($argv[2]) ? $argv[2] : 8480;
$sock = stream_socket_client("tcp://$ip:$port");
if(!$sock)exit("can not create sock\n");

fwrite($sock, JsonProtocol::encode(array("action"=>'connect',"uid"=>27)));


$ret = read_data($sock);
if(isset($ret['uid']))
{
    echo "chart room login success , your uid is [{$ret['uid']}]\n";
    echo "use uid:words send message to one user\n";
    echo "use words send message to all\n";
}
else
{
    exit("connet faild reponse:$rsp_string\n");
}

$MYUID = $ret['uid'];

stream_set_blocking($sock, 0);
stream_set_blocking(STDIN, 0);

$read = array(STDIN, $sock);

$write = $ex = array();

$game_id=1;
$section_id =10;
$desk_id =0;
$seat = 0;
$rand_str="";
$other_call = "";

while(1)
{
    $read_copy = $read;
    if($ret = stream_select($read_copy, $write, $ex, 1000))
    {
       foreach($read as $fd)
       {
          // 接收消息
          if((int)$fd === (int)$sock)
          {
              $ret = read_data($fd);
              if(!$ret){continue;exit("connection closed\n ");}
              
              // 是服务端发来的心跳，只是检测联通性，不用回复
              if("#ping#" == $ret)
              {
                  continue;
              }
              
              $action = $ret["action"];
              switch ($action){
                  case "get_all_desks":
                      print_r($ret["lists"]);
                      break;
                  case "add_desk_other":
                        echo "right_user come:".$ret["come_uid"]."\n";
                     break;
                  case "add_desk":
                      $desk_id = $ret["desk_id"];
                      $seat= $ret["seat"];
                      echo "add_desk_succ\n";
                      break;
                  case "game_set_gold_suc":
                      echo "game_set_gold_suc\n";
                      break;
                  case "all_start":
                      echo "all_start\n";
                      break;
                  case "game_start":
                      $rand_str = $ret["data_str"];
                      $call = $ret["call"];
                      if(!$call){
                        echo "game_start !Your data_str is:".$rand_str."\n";
                      }else{
                           echo "game_start you first!Your data_str is:".$rand_str."\n";
                      }
                      break;
                  case "game_other_call":
                      $other_call = $ret["call"];
                      $call= 1;
                      echo "other call :".$other_call;
                      break;
                  case "game_over":
                      $vectory= $ret["vectory"];
                      if($vectory){
                          echo "You are win!\n";
                      }else{
                          echo "You lost!\n";
                      }
                      break;
                  case "other_user_exit":
                      echo "other user exit";
                      break;
                  case "game_add_gold":
                      echo "other add gold";
                      break;
                  case "game_add_gold_suc":
                      echo "other confirm add gold!";
                      break;
                  case "other_call_gold":
                      echo "other need gold!";
                      break;
                  case "game_other_given":
                      echo "other give me gold!";
                      break;
                  case "game_given_gold_succ":
                      echo "give other gold succ!";
                      break;
                  
              }
              continue;
          }
          
          // 向某个uid发送消息 格式为 uid:xxxxxxxx
          $ret = fgets(STDIN, 10240);
          if(!$ret)continue;
          $params = explode(":", $ret);
          $action = $params[0];
          switch ($action) {
              case "get_all_desks":
                  fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID, "action"=>$action,"game_id"=>$game_id, "section_id"=>$section_id)));
                  break;
              case "add_desk":
                  $desk_id = $params[1];
                  fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID, "action"=>$action, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                  break;
              case "game_set_gold":
                  fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID, "action"=>$action, "gold"=>$params[1], "seat"=>$seat, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                  break;
              case "game_start":
                  fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID, "action"=>$action, "seat"=>$seat, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                  break;
              
              case "game_call":
                  if(!$call){
                      echo "You should wait!\n";
                  }else{
                      $call = 0;
                      fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID, "call"=>$params[1], "action"=>$action, "seat"=>$seat, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                  }
                  break;
              case "game_end":
                 if(!$call){
                      echo "You should wait!";
                  }else{
                      $call = 0;
                      fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID, "call"=>$other_call, "action"=>$action, "seat"=>$seat, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                  }
                  break;
             case "game_exit":
                fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID,  "action"=>$action, "seat"=>$seat, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                 break;
              case "game_add_gold":
                  fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID,  "action"=>$action,"add_gold"=>$params[1], "seat"=>$seat, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                 break;
              case "game_add_gold_suc":
                  fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID,  "action"=>$action,"seat"=>$seat,"add_gold"=>$params[2],"succ"=>$params[1],"game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                  break;
              case "game_call_gold":
                  fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID,  "action"=>$action,"seat"=>$seat, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                  break;
              case "game_given_gold":
                  fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID,  "action"=>$action,"given_gold"=>$params[1],"seat"=>$seat, "game_id"=>$game_id,"desk_id"=>$desk_id, "section_id"=>$section_id)));
                  break;
             default:
                  break;
          }
          /*
          if(preg_match("/(\d+):(.*)/", $ret, $match))
          {
             $uid = $match[1];
             $words = $match[2];
             fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID, 'to_uid'=>$uid, 'message'=>$words)));
             continue;
          }*/
          // 向所有用户发消息
         // fwrite($sock, JsonProtocol::encode(array('from_uid'=>$MYUID, 'to_uid'=>'all', 'message'=>$ret)));
          continue;

       }
    }
}

function read_data($socket)
{
   $buf = stream_socket_recvfrom($socket, 4);
   if($buf == '')
   {
       return;
   }
   $remain_len = JsonProtoCol::check($buf);
   //echo '$remain_len:'.$buf.':'.bin2hex($buf).":".$remain_len."\n";
   $buf .= stream_socket_recvfrom($socket, $remain_len);
   stream_set_blocking($socket, 0);
   //echo '$buf:'.$buf."\n";
   return JsonProtocol::decode($buf);
}
